package comviva.msisdn;


public class IntHashMap {
	static final int DEFAULT_INITIAL_CAPACITY = 16;
	static final int MAXIMUM_CAPACITY = 1 << 30;
	static final float DEFAULT_LOAD_FACTOR = 0.75f;
	transient Entry[] table;
	transient int size;
	int threshold;
	final float loadFactor;
	transient volatile int modCount;

	public IntHashMap(int initialCapacity) {

		// Find a power of 2 >= initialCapacity
		int capacity = 1;
		while (capacity < initialCapacity)
			capacity <<= 1;

		this.loadFactor = 0.75f;
		threshold = (int) (capacity * loadFactor);
		table = new Entry[capacity];
	}

	public int put(int key, int value) {
		int hash = hash(key);
        int i = indexFor(hash, table.length);
        for (Entry e = table[i]; e != null; e = e.next) {
            int k;
            if (e.hash == hash && ((k = e.key) == key)) {
                int oldValue = e.value;
                e.value = value;
                //e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        addEntry(hash, key, value, i);
        return -1;
	}

	static int hash(int h) {
		// This function ensures that hashCodes that differ only by
		// constant multiples at each bit position have a bounded
		// number of collisions (approximately 8 at default load factor).
		h ^= (h >>> 20) ^ (h >>> 12);
		return h ^ (h >>> 7) ^ (h >>> 4);
	}

	/**
	 * Returns index for hash code h.
	 */
	static int indexFor(int h, int length) {
		return h & (length - 1);
	}

	void addEntry(int hash, int key, int value, int bucketIndex) {
		Entry e = table[bucketIndex];
		table[bucketIndex] = new Entry(hash, key, value, e);
		if (size++ >= threshold)
			resize(2 * table.length);
	}

	void resize(int newCapacity) {
		Entry[] oldTable = table;
		int oldCapacity = oldTable.length;
		if (oldCapacity == MAXIMUM_CAPACITY) {
			threshold = Integer.MAX_VALUE;
			return;
		}

		Entry[] newTable = new Entry[newCapacity];
		transfer(newTable);
		table = newTable;
		threshold = (int) (newCapacity * loadFactor);
	}

	void transfer(Entry[] newTable) {
		Entry[] src = table;
		int newCapacity = newTable.length;
		for (int j = 0; j < src.length; j++) {
			Entry e = src[j];
			if (e != null) {
				src[j] = null;
				do {
					Entry next = e.next;
					int i = indexFor(e.hash, newCapacity);
					e.next = newTable[i];
					newTable[i] = e;
					e = next;
				} while (e != null);
			}
		}
	}

	public boolean containsKey(int key) {
		return getEntry(key) != null;
	}

	final Entry getEntry(int key) {
		int hash = hash(key);
		for (Entry e = table[indexFor(hash, table.length)]; e != null; e = e.next) {
			int k;
			if (e.hash == hash
					&& ((k = e.key) == key ))
				return e;
		}
		return null;
	}
	
	static class Entry {
        final int key;
        int value;
        Entry next;
        final int hash;

        /**
         * Creates new entry.
         */
        Entry(int h, int k, int v, Entry n) {
            value = v;
            next = n;
            key = k;
            hash = h;
        }

        public final int getKey() {
            return key;
        }

        public final int getValue() {
            return value;
        }

        public final int setValue(int newValue) {
	    int oldValue = value;
            value = newValue;
            return oldValue;
        }

        public final boolean equals(Object o) {
            if (!(o instanceof Entry))
                return false;
            Entry e = (Entry)o;
            int k1 = getKey();
            int k2 = e.getKey();
            if (k1 == k2) {
                int v1 = getValue();
                int v2 = e.getValue();
                if (v1 == v2 )
                    return true;
            }
            return false;
        }

        public final int hashCode() {
        	return key ^ value;
        }

        public final String toString() {
            return getKey() + "=" + getValue();
        }
    }

}
